package com.smartcommunity.utils;

import com.github.pagehelper.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.Period;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
public class DateUtil {
    public static void main(String[] args) {
        double interval = (double)900 * 0.666666666666d;
        double max = Math.max(interval, 1.0D);
        int ttlMinus1 = 900 - 1;
        double min = Math.min((double)ttlMinus1, max);
        System.out.println(Math.round(1000.0D * min));
    }
    public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
    public static final String DATE_SIMPLE_FORMAT = "yyyy-MM-dd";
    public static final String DATE_FULL_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FULL_FORMAT_START = "yyyy-MM-dd 00:00:00";
    public static final String DATE_FULL_FORMAT_END = "yyyy-MM-dd 23:59:59";
    public static final String DATE_HOUR_FORMAT = "yyyy-MM-dd HH";
    public static final String DATE_HOUR_ONLY = "HH";
    public static final String DATE_MONTH_FORMAT = "yyyy-MM";
    public static final String TIME_SIMPLE_FORMAT = "HH:mm:ss";
    public static final String DATE_YEAR_FORMAT = "yyyy";
    public static final String DATE_FIXED_FORMAT = "yyyyMMddHHmmssSSS";
    public static final SimpleDateFormat DATE_FORMAT_S = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    public static final SimpleDateFormat DATE_SIMPLE_FORMAT_S = new SimpleDateFormat("yyyy-MM-dd");
    public static final SimpleDateFormat DATE_HOUR_FORMAT_S = new SimpleDateFormat("yyyy-MM-dd HH");
    public static final SimpleDateFormat DATE_FULL_FORMAT_S = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static final SimpleDateFormat DATE_MONTH_FORMAT_S = new SimpleDateFormat("yyyy-MM");
    public static final SimpleDateFormat DATE_YEAR_FORMAT_S = new SimpleDateFormat("yyyy");

    public DateUtil() {
    }

    public static long parseDate(String date, String format) throws ParseException {
        return toDate(date, format).getTime();
    }
    public static Date getWeekStart(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        return cal.getTime();
    }
    public static Date getWeekEnd(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0)
            day_of_week = 7;
        c.add(Calendar.DATE, -day_of_week + 7);
        return c.getTime();
    }

    public static Date toDate(String date, String format) {
        if ("".equals(format) || format == null) {
            format = "yyyy-MM-dd HH:mm:ss.SSS";
        }

        try {
            format = StringUtil.isEmpty(format) ? "yyyy-MM-dd HH:mm:ss.SSS" : format;
            return (new SimpleDateFormat(format)).parse(date);
        } catch (ParseException var3) {
            log.error("DateUtil:toDate error,date:{},format:{}",date,format);
            return getCurrentDate();
        }
    }

    public static Date toDate(String date) {
        try {
            return (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).parse(date);
        } catch (ParseException var2) {
            log.error("DateUtil:toDate error,date:{}",date);
            return getCurrentDate();
        }
    }

    public static Date toSafeDate(String date, String format) {
        return toDate(date, format);
    }

    public static Date toDate(long time) {
        return new Date(time);
    }

    public static String getCurrentDate(String format) {
        if (StringUtil.isEmpty(format)) {
            format = "yyyy-MM-dd HH:mm:ss.SSS";
        }

        return toString(new Date(), format);
    }

    public static Date getCurrentDate() {
        return new Date();
    }

    public static String toSimpleDate(Date date) {
        return toString(date, "yyyy-MM-dd");
    }

    public static String toFullDate(Date date) {
        return toString(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static String toString(Date date, String format) {
        return (new SimpleDateFormat(format)).format(date);
    }

    public static String getDate(long timeStamp) {
        return getDate(timeStamp, "yyyy-MM-dd HH:mm:ss.SSS");
    }

    public static String getDate(long timeStamp, String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        return df.format(new Date(timeStamp));
    }

    public static Date parseDate(String date) {
        if (date == null) {
            return null;
        } else {
            String[] formats = new String[]{"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd", "yyyy-MM"};
            SimpleDateFormat format = new SimpleDateFormat();
            String[] arr$ = formats;
            int len$ = formats.length;
            int i$ = 0;

            while (i$ < len$) {
                String fmt = arr$[i$];
                format.applyPattern(fmt);

                try {
                    return format.parse(date);
                } catch (Exception var9) {
                    ++i$;
                }
            }

            try {
                return new Date(Long.parseLong(date));
            } catch (Exception var8) {
                log.error("DateUtil:parseDate error,date:{}",date,var8);
                return null;
            }
        }
    }

    public static Date getDateStart(Date date) {
        if (null == date) {
            date = getCurrentDate();
        }

        return getDateTime(date, 0, 0, 0);
    }

    public static Date getDateEnd(Date date) {
        if (null == date) {
            date = getCurrentDate();
        }

        return getDateTime(date, 23, 59, 59);
    }

    public static Date getMonthStart(Date date) {
        return getMonthStart(date, false);
    }

    public static Date getMonthStart(Date date, boolean reset) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(5, 1);
        if (reset) {
            calendar.set(10, 0);
            calendar.set(12, 0);
            calendar.set(13, 0);
        }

        return calendar.getTime();
    }

    public static Date addSecond(Date date, Integer seconds) {
        return addTime(date, 13, seconds);
    }

    public static Date addHour(Date date, Integer hours) {
        return addTime(date, 11, hours);
    }

    public static Date addDays(Date date, Integer days) {
        return addTime(date, 6, days);
    }

    public static Date addMonths(Date date, Integer Months) {
        return addTime(date, 2, Months);
    }

    public static Date addYears(Date date, Integer Years) {
        return addTime(date, 1, Years);
    }

    public static Date addTime(Date date, int field, int value) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(field, value);
        return calendar.getTime();
    }

    private static Date getDateTime(Date date, int hour, int minute, int second) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(10, hour);
        calendar.set(12, minute);
        calendar.set(13, second);
        return calendar.getTime();
    }

    public static Date getMinDate() {
        String strDate = "0001-01-01 00:00:00";
        String strDateFormat = "yyyy-MM-dd HH:mm:ss";
        Date minDate = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(strDateFormat);

        try {
            minDate = dateFormat.parse(strDate);
        } catch (ParseException var5) {
            log.error("DateUtil:getMinDate error,",var5);
        }

        return minDate;
    }

    public static boolean isLeapYear(String date) {
        try {
            Date d = toDate(date, "yyyy-MM-dd");
            GregorianCalendar gc = (GregorianCalendar) Calendar.getInstance();
            gc.setTime(d);
            int year = gc.get(1);
            if (year % 400 == 0) {
                return true;
            } else if (year % 4 == 0) {
                return year % 100 != 0;
            } else {
                return false;
            }
        } catch (Exception var4) {
            log.error("DateUtil:isLeapYear error,",var4);
            return false;
        }
    }

    public static int compareDate(Date date1, Date date2) {
        if (date1.getTime() > date2.getTime()) {
            return 1;
        } else {
            return date1.getTime() < date2.getTime() ? -1 : 0;
        }
    }

    public static Date getCurrentWeekDayStartTime() {
        Calendar c = Calendar.getInstance();

        try {
            int weekday = c.get(7) - 2;
            c.add(5, -weekday);
            c.setTime(DATE_FULL_FORMAT_S.parse(DATE_SIMPLE_FORMAT_S.format(c.getTime()) + " 00:00:00"));
        } catch (Exception var2) {
            log.error("DateUtil:getCurrentWeekDayStartTime error,",var2);
        }

        return c.getTime();
    }

    public static Date getCurrentWeekDayEndTime() {
        Calendar c = Calendar.getInstance();

        try {
            int weekday = c.get(7);
            c.add(5, 8 - weekday);
            c.setTime(DATE_FULL_FORMAT_S.parse(DATE_SIMPLE_FORMAT_S.format(c.getTime()) + " 23:59:59"));
        } catch (Exception var2) {
            log.error("DateUtil:getCurrentWeekDayEndTime error,",var2);
        }

        return c.getTime();
    }

    public static Date getCurrentDayStartTime() {
        Date now = new Date();

        try {
            now = DATE_SIMPLE_FORMAT_S.parse(DATE_SIMPLE_FORMAT_S.format(now));
        } catch (Exception var2) {
            log.error("DateUtil:getCurrentDayStartTime error,",var2);
        }

        return now;
    }

    public static Date getCurrentDayEndTime() {
        Date now = new Date();

        try {
            now = DATE_FULL_FORMAT_S.parse(DATE_SIMPLE_FORMAT_S.format(now) + " 23:59:59");
        } catch (Exception var2) {
            log.error("DateUtil:getCurrentDayEndTime error,",var2);
        }

        return now;
    }

    public static Date getCurrentHourStartTime() {
        Date now = new Date();

        try {
            now = DATE_HOUR_FORMAT_S.parse(DATE_HOUR_FORMAT_S.format(now));
        } catch (Exception var2) {
            log.error("DateUtil:getCurrentHourStartTime error,",var2);
        }

        return now;
    }

    public static Date getCurrentHourEndTime() {
        Date now = new Date();

        try {
            now = DATE_FULL_FORMAT_S.parse(DATE_HOUR_FORMAT_S.format(now) + ":59:59");
        } catch (Exception var2) {
            log.error("DateUtil:getCurrentHourEndTime error,",var2);
        }

        return now;
    }

    public static Date getCurrentMonthStartTime() {
        Calendar c = Calendar.getInstance();
        Date now = null;

        try {
            c.set(5, 1);
            now = DATE_SIMPLE_FORMAT_S.parse(DATE_SIMPLE_FORMAT_S.format(c.getTime()));
        } catch (Exception var3) {
            log.error("DateUtil:getCurrentMonthStartTime error,",var3);
        }

        return now;
    }

    public static Date getCurrentMonthEndTime() {
        Calendar c = Calendar.getInstance();
        Date now = null;

        try {
            c.set(5, 1);
            c.add(2, 1);
            c.add(5, -1);
            now = DATE_FULL_FORMAT_S.parse(DATE_SIMPLE_FORMAT_S.format(c.getTime()) + " 23:59:59");
        } catch (Exception var3) {
            log.error("DateUtil:getCurrentMonthEndTime error,",var3);
        }

        return now;
    }

    public static Date getCurrentYearStartTime() {
        Calendar c = Calendar.getInstance();
        Date now = null;

        try {
            c.set(2, 0);
            c.set(5, 1);
            now = DATE_SIMPLE_FORMAT_S.parse(DATE_SIMPLE_FORMAT_S.format(c.getTime()));
        } catch (Exception var3) {
            log.error("DateUtil:getCurrentYearStartTime error,",var3);
        }

        return now;
    }

    public static Date getCurrentYearEndTime() {
        Calendar c = Calendar.getInstance();
        Date now = null;

        try {
            c.set(2, 11);
            c.set(5, 31);
            now = DATE_FULL_FORMAT_S.parse(DATE_SIMPLE_FORMAT_S.format(c.getTime()) + " 23:59:59");
        } catch (Exception var3) {
            log.error("DateUtil:getCurrentYearEndTime error,",var3);
        }

        return now;
    }

    public static Date getCurrentQuarterStartTime() {
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(2) + 1;
        Date now = null;

        try {
            if (currentMonth >= 1 && currentMonth <= 3) {
                c.set(2, 0);
            } else if (currentMonth >= 4 && currentMonth <= 6) {
                c.set(2, 3);
            } else if (currentMonth >= 7 && currentMonth <= 9) {
                c.set(2, 4);
            } else if (currentMonth >= 10 && currentMonth <= 12) {
                c.set(2, 9);
            }

            c.set(5, 1);
            now = DATE_FULL_FORMAT_S.parse(DATE_SIMPLE_FORMAT_S.format(c.getTime()) + " 00:00:00");
        } catch (Exception var4) {
            log.error("DateUtil:getCurrentQuarterStartTime error,",var4);
        }

        return now;
    }

    public static Date getHalfYearStartTime() {
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(2) + 1;
        Date now = null;

        try {
            if (currentMonth >= 1 && currentMonth <= 6) {
                c.set(2, 0);
            } else if (currentMonth >= 7 && currentMonth <= 12) {
                c.set(2, 6);
            }

            c.set(5, 1);
            now = DATE_FULL_FORMAT_S.parse(DATE_SIMPLE_FORMAT_S.format(c.getTime()) + " 00:00:00");
        } catch (Exception var4) {
            log.error("DateUtil:getHalfYearStartTime error,",var4);
        }

        return now;
    }

    public static Date getHalfYearEndTime() {
        Calendar c = Calendar.getInstance();
        int currentMonth = c.get(2) + 1;
        Date now = null;

        try {
            if (currentMonth >= 1 && currentMonth <= 6) {
                c.set(2, 5);
                c.set(5, 30);
            } else if (currentMonth >= 7 && currentMonth <= 12) {
                c.set(2, 11);
                c.set(5, 31);
            }

            now = DATE_FULL_FORMAT_S.parse(DATE_SIMPLE_FORMAT_S.format(c.getTime()) + " 23:59:59");
        } catch (Exception var4) {
            log.error("DateUtil:getHalfYearEndTime error,",var4);
        }

        return now;
    }

    public static Date getIntStartDate() {
        Date now = null;
        Calendar cal = Calendar.getInstance();
        cal.add(5, -1);

        try {
            now = DATE_SIMPLE_FORMAT_S.parse(DATE_SIMPLE_FORMAT_S.format(cal.getTime()));
        } catch (ParseException var3) {
            log.error("DateUtil:getIntStartDate error,",var3);
        }

        return now;
    }

    public static Date getIntENdDate() {
        Calendar cal = Calendar.getInstance();
        cal.add(5, -1);
        Date now = null;

        try {
            now = getSimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(getSimpleDateFormat("yyyy-MM-dd").format(cal.getTime()) + " 23:59:59");
        } catch (ParseException var3) {
            log.error("DateUtil:getIntENdDate error,",var3);
        }

        return now;
    }

    private static SimpleDateFormat getSimpleDateFormat(String format) {
        return new SimpleDateFormat(format);
    }

    public static String getBeforeTime(int hour, int minute, String formatTimeType) {
        String formatType = "yyyy-MM-dd HH:mm:ss";
        if (!StringUtil.isEmpty(formatTimeType)) {
            formatType = formatTimeType;
        }

        SimpleDateFormat format = new SimpleDateFormat(formatType);
        Calendar calendar = Calendar.getInstance();
        calendar.add(11, hour);
        calendar.add(12, minute);
        return format.format(calendar.getTime());
    }

    /**
     * 获取过去第几天的日期
     *
     * @param past
     * @return
     */
    public static String getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today) +" 00:00:00";
        return result;
    }

    /**
     * 获取未来 第 past 天的日期
     *
     * @param past
     * @return
     */
    public static String getFetureDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        String result = format.format(today);
        return result;
    }


    /*
     * 将时间戳转换为时间
     */
    public static String stampToDate(String stap) {
        String time;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(stap);
        Date date = new Date(lt);
        time = simpleDateFormat.format(date);
        return time;
    }

    /**
     * 将时间转换为时间戳
     *
     * @param time
     * @return
     * @throws ParseException
     */
    public static Long dateToStamp(String time) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(time);
        return date.getTime();//获取时间的时间戳
    }

}
